package aom.ju.ss.utils.provinceCityDistrict

import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.ProvinceCityDistrict
import aom.ju.ss.mode.ProvinceData
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.SpUtils
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.provinceCityDistrict.*
import aom.ju.ss.view.dialog.TitleDialog
import com.google.gson.Gson
import aom.ju.ss.utils.show
import java.util.*


/**
 * 获取
 */
class ShowPCADialogUtils(
    var activity: Activity,
    var area_id: String? = null,
    var bt: TextView, //省市区
    var et_street: TextView?,
    back: OnCustomCityPickerItemClickListener
) {
    var lists: MutableList<ProvinceData> = arrayListOf()
    var back: OnCustomCityPickerItemClickListener? = back

    init {


        //关闭键盘
        val view = activity.window.peekDecorView()
        if (view != null) {
            val inputmanger =
                activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputmanger.hideSoftInputFromWindow(view.windowToken, 0)
        }


        LogUtils.e("SpUtils.getString(\"REGION\")" + SpUtils.getString("REGION"))
        if ("" == SpUtils.getString("REGION")) {
            OKHttpUtils(activity)
                .SetApiUrl(LP_API.region_list)
                .POST(object : MyHttpCallBack {
                    override fun ok(jsonString: String?, httpTY: String?) {
                        val mode = Gson().fromJson(jsonString, ProvinceCityDistrict::class.java)
                        SpUtils.putString("REGION", Gson().toJson(mode))
                        lists.addAll(mode.data)
                        bt.setOnClickListener {
                            if (lists.size == 0) {
                                TitleDialog.showMsg(activity, "正在获取省市区数据")
                                return@setOnClickListener
                            }

                            showPicker()
                        }

                    }

                    override fun error(e: String?, jsonString: String?, httpTY: String?) {
                        e?.show()
                        bt.setOnClickListener {
                            TitleDialog.showMsg(activity, "获取省市区数据失败")
                        }

                    }

                })

        } else {
            LogUtils.e(SpUtils.getString("REGION"))

            val mode =
                Gson().fromJson(SpUtils.getString("REGION"), ProvinceCityDistrict::class.java)
            lists.addAll(mode.data)
            bt.setOnClickListener {
                if (lists.size == 0) {
                    TitleDialog.showMsg(activity, "正在获取省市区数据")
                    return@setOnClickListener
                }

                showPicker()
            }
        }



        LogUtils.e("area_id==$area_id")
        checkStreet(area_id)

    }

    private fun checkStreet(areaId: String?) {
        et_street?.let {
            StreetSelectionTool(activity, areaId, it,
                object : streetIdCall {
                    override fun ok(street_id: String, street: String) {
                        back?.onSelectedStreet(street_id, street)
                    }
                })
        }

    }

    private fun showPicker() {
        val mProvinceListData: MutableList<CustomCityData> = ArrayList()
        for (addressBean in lists) {
            val pro = CustomCityData(addressBean.id, addressBean.name, addressBean.pid)
            val cityList: MutableList<CustomCityData> = ArrayList()
            for (addressSonBean in addressBean.citylist) {
                val city = CustomCityData(addressSonBean.id, addressSonBean.name, addressBean.pid)
                val distList: MutableList<CustomCityData> = ArrayList()
                for (sonBean in addressSonBean.arealist) {
                    distList.add(CustomCityData(sonBean.id, sonBean.name, addressBean.pid))
                }
                city.list = distList
                cityList.add(city)
            }
            pro.list = cityList
            mProvinceListData.add(pro)
        }

        CustomCityPickerDialog(activity,mProvinceListData,object :
            OnCustomCityPickerItemClickListener() {
            override fun onSelected(
                province: CustomCityData?,
                city: CustomCityData?,
                district: CustomCityData?
            ) {
                super.onSelected(province, city, district)
                if (province != null && city != null && district != null) {
                    val sb = StringBuilder()
                    sb.append(province.name)
                    sb.append(city.name)
                    sb.append(district.name)
                    bt.text = sb.toString()

                    checkStreet(district.id)

                    LogUtils.e("district .id==" + district.id)
                    back?.onSelected(province, city, district)
                    back?.onSelectedStreet("", "")
                    et_street?.text = ""
                } else {
                    bt.text = "请选择区域"
                }
            }
        })


//        val cityConfig = CustomConfig.Builder()
//            .title("选择城市")
//            .visibleItemsCount(10)
//            .setCityData(mProvinceListData)
//            .provinceCyclic(true)
//            .cityCyclic(false)
//            .districtCyclic(true)
//            .drawShadows(true)
//            .setCityWheelType(type)
//            .build()
//        customCityPicker.setCustomConfig(cityConfig)
//        customCityPicker.setOnCustomCityPickerItemClickListener(object :
//            OnCustomCityPickerItemClickListener() {
//            override fun onSelected(
//                province: CustomCityData?,
//                city: CustomCityData?,
//                district: CustomCityData?
//            ) {
//                super.onSelected(province, city, district)
//                if (province != null && city != null && district != null) {
//                    val sb = StringBuilder()
//                    sb.append(province.name)
//                    sb.append(city.name)
//                    sb.append(district.name)
//                    bt.text = sb.toString()
//
//                    checkStreet(district.id)
//
//                    LogUtils.e("district .id==" + district.id)
//                    back?.onSelected(province, city, district)
//                    back?.onSelectedStreet("", "")
//                    et_street?.text = ""
//                } else {
//                    bt.text = "请选择区域"
//                }
//
//            }
//        })

    }

}